
USE [HIV_Schema]
GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.TrangThai_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.TrangThai_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.TrangThai_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets all records from the TrangThai table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.TrangThai_Get_List

AS


				
				SELECT
					[Id],
					[LoaiId],
					[Ma],
					[GhiChu]
				FROM
					[dbo].[TrangThai]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.TrangThai_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.TrangThai_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.TrangThai_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets records from the TrangThai table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.TrangThai_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Id])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [Id]'
				SET @SQL = @SQL + ' FROM [dbo].[TrangThai]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[Id], O.[LoaiId], O.[Ma], O.[GhiChu]
				FROM
				    [dbo].[TrangThai] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Id] = PageIndex.[Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[TrangThai]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.TrangThai_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.TrangThai_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.TrangThai_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Inserts a record into the TrangThai table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.TrangThai_Insert
(

	@Id int   ,

	@LoaiId int   ,

	@Ma varchar (30)  ,

	@GhiChu nvarchar (255)  
)
AS


				
				INSERT INTO [dbo].[TrangThai]
					(
					[Id]
					,[LoaiId]
					,[Ma]
					,[GhiChu]
					)
				VALUES
					(
					@Id
					,@LoaiId
					,@Ma
					,@GhiChu
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.TrangThai_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.TrangThai_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.TrangThai_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Updates a record in the TrangThai table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.TrangThai_Update
(

	@Id int   ,

	@OriginalId int   ,

	@LoaiId int   ,

	@Ma varchar (30)  ,

	@GhiChu nvarchar (255)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[TrangThai]
				SET
					[Id] = @Id
					,[LoaiId] = @LoaiId
					,[Ma] = @Ma
					,[GhiChu] = @GhiChu
				WHERE
[Id] = @OriginalId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.TrangThai_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.TrangThai_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.TrangThai_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Deletes a record in the TrangThai table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.TrangThai_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[TrangThai] WITH (ROWLOCK) 
				WHERE
					[Id] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.TrangThai_GetByLoaiId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.TrangThai_GetByLoaiId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.TrangThai_GetByLoaiId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Select records from the TrangThai table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.TrangThai_GetByLoaiId
(

	@LoaiId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[Id],
					[LoaiId],
					[Ma],
					[GhiChu]
				FROM
					[dbo].[TrangThai]
				WHERE
					[LoaiId] = @LoaiId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.TrangThai_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.TrangThai_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.TrangThai_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Select records from the TrangThai table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.TrangThai_GetById
(

	@Id int   
)
AS


				SELECT
					[Id],
					[LoaiId],
					[Ma],
					[GhiChu]
				FROM
					[dbo].[TrangThai]
				WHERE
					[Id] = @Id
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.TrangThai_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.TrangThai_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.TrangThai_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Finds records in the TrangThai table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.TrangThai_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@LoaiId int   = null ,

	@Ma varchar (30)  = null ,

	@GhiChu nvarchar (255)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Id]
	, [LoaiId]
	, [Ma]
	, [GhiChu]
    FROM
	[dbo].[TrangThai]
    WHERE 
	 ([Id] = @Id OR @Id IS NULL)
	AND ([LoaiId] = @LoaiId OR @LoaiId IS NULL)
	AND ([Ma] = @Ma OR @Ma IS NULL)
	AND ([GhiChu] = @GhiChu OR @GhiChu IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Id]
	, [LoaiId]
	, [Ma]
	, [GhiChu]
    FROM
	[dbo].[TrangThai]
    WHERE 
	 ([Id] = @Id AND @Id is not null)
	OR ([LoaiId] = @LoaiId AND @LoaiId is not null)
	OR ([Ma] = @Ma AND @Ma is not null)
	OR ([GhiChu] = @GhiChu AND @GhiChu is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.BenhNhanDangKy_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.BenhNhanDangKy_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.BenhNhanDangKy_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets all records from the BenhNhanDangKy table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.BenhNhanDangKy_Get_List

AS


				
				SELECT
					[BenhNhanId],
					[MaDonVi],
					[MaBenhNhan],
					[NgayDangKy],
					[NgayTao],
					[UserTao],
					[NgaySua],
					[UserSua],
					[TenBenhNhan],
					[NgaySinh],
					[GioiTinh],
					[Cmnd],
					[NgayCapCmnd],
					[NoiCapCmnd],
					[ThTruDuong],
					[ThTruPhuong],
					[ThTruQuan],
					[ThTruTp],
					[TmTruDuong],
					[TmTruPhuong],
					[TmTruQuan],
					[TmTruTp],
					[DienThoai],
					[Email],
					[HinhAnh],
					[VanTay],
					[GhiChu],
					[NgayDuyet],
					[UserDuyet],
					[GhiChuDuyet],
					[NgayDangKyHiv],
					[MaNgheNghiep],
					[DoiTuongId],
					[NguyCoLayNhiemId],
					[MaXetNghiem],
					[MaDuyet],
					[TrangThaiId],
					[DeleteFlag]
				FROM
					[dbo].[BenhNhanDangKy]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.BenhNhanDangKy_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.BenhNhanDangKy_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.BenhNhanDangKy_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets records from the BenhNhanDangKy table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.BenhNhanDangKy_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [BenhNhanId] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([BenhNhanId])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [BenhNhanId]'
				SET @SQL = @SQL + ' FROM [dbo].[BenhNhanDangKy]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[BenhNhanId], O.[MaDonVi], O.[MaBenhNhan], O.[NgayDangKy], O.[NgayTao], O.[UserTao], O.[NgaySua], O.[UserSua], O.[TenBenhNhan], O.[NgaySinh], O.[GioiTinh], O.[Cmnd], O.[NgayCapCmnd], O.[NoiCapCmnd], O.[ThTruDuong], O.[ThTruPhuong], O.[ThTruQuan], O.[ThTruTp], O.[TmTruDuong], O.[TmTruPhuong], O.[TmTruQuan], O.[TmTruTp], O.[DienThoai], O.[Email], O.[HinhAnh], O.[VanTay], O.[GhiChu], O.[NgayDuyet], O.[UserDuyet], O.[GhiChuDuyet], O.[NgayDangKyHiv], O.[MaNgheNghiep], O.[DoiTuongId], O.[NguyCoLayNhiemId], O.[MaXetNghiem], O.[MaDuyet], O.[TrangThaiId], O.[DeleteFlag]
				FROM
				    [dbo].[BenhNhanDangKy] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[BenhNhanId] = PageIndex.[BenhNhanId]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[BenhNhanDangKy]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.BenhNhanDangKy_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.BenhNhanDangKy_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.BenhNhanDangKy_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Inserts a record into the BenhNhanDangKy table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.BenhNhanDangKy_Insert
(

	@BenhNhanId int    OUTPUT,

	@MaDonVi varchar (15)  ,

	@MaBenhNhan nvarchar (20)  ,

	@NgayDangKy datetime   ,

	@NgayTao datetime   ,

	@UserTao nvarchar (30)  ,

	@NgaySua datetime   ,

	@UserSua nvarchar (30)  ,

	@TenBenhNhan nvarchar (30)  ,

	@NgaySinh datetime   ,

	@GioiTinh int   ,

	@Cmnd nvarchar (15)  ,

	@NgayCapCmnd datetime   ,

	@NoiCapCmnd nvarchar (50)  ,

	@ThTruDuong nvarchar (50)  ,

	@ThTruPhuong int   ,

	@ThTruQuan int   ,

	@ThTruTp int   ,

	@TmTruDuong nvarchar (50)  ,

	@TmTruPhuong int   ,

	@TmTruQuan int   ,

	@TmTruTp int   ,

	@DienThoai nvarchar (15)  ,

	@Email nvarchar (255)  ,

	@HinhAnh image   ,

	@VanTay nvarchar (256)  ,

	@GhiChu nvarchar (255)  ,

	@NgayDuyet datetime   ,

	@UserDuyet nvarchar (30)  ,

	@GhiChuDuyet nvarchar (255)  ,

	@NgayDangKyHiv datetime   ,

	@MaNgheNghiep char (3)  ,

	@DoiTuongId int   ,

	@NguyCoLayNhiemId int   ,

	@MaXetNghiem nvarchar (20)  ,

	@MaDuyet varchar (2)  ,

	@TrangThaiId int   ,

	@DeleteFlag bit   
)
AS


				
				INSERT INTO [dbo].[BenhNhanDangKy]
					(
					[MaDonVi]
					,[MaBenhNhan]
					,[NgayDangKy]
					,[NgayTao]
					,[UserTao]
					,[NgaySua]
					,[UserSua]
					,[TenBenhNhan]
					,[NgaySinh]
					,[GioiTinh]
					,[Cmnd]
					,[NgayCapCmnd]
					,[NoiCapCmnd]
					,[ThTruDuong]
					,[ThTruPhuong]
					,[ThTruQuan]
					,[ThTruTp]
					,[TmTruDuong]
					,[TmTruPhuong]
					,[TmTruQuan]
					,[TmTruTp]
					,[DienThoai]
					,[Email]
					,[HinhAnh]
					,[VanTay]
					,[GhiChu]
					,[NgayDuyet]
					,[UserDuyet]
					,[GhiChuDuyet]
					,[NgayDangKyHiv]
					,[MaNgheNghiep]
					,[DoiTuongId]
					,[NguyCoLayNhiemId]
					,[MaXetNghiem]
					,[MaDuyet]
					,[TrangThaiId]
					,[DeleteFlag]
					)
				VALUES
					(
					@MaDonVi
					,@MaBenhNhan
					,@NgayDangKy
					,@NgayTao
					,@UserTao
					,@NgaySua
					,@UserSua
					,@TenBenhNhan
					,@NgaySinh
					,@GioiTinh
					,@Cmnd
					,@NgayCapCmnd
					,@NoiCapCmnd
					,@ThTruDuong
					,@ThTruPhuong
					,@ThTruQuan
					,@ThTruTp
					,@TmTruDuong
					,@TmTruPhuong
					,@TmTruQuan
					,@TmTruTp
					,@DienThoai
					,@Email
					,@HinhAnh
					,@VanTay
					,@GhiChu
					,@NgayDuyet
					,@UserDuyet
					,@GhiChuDuyet
					,@NgayDangKyHiv
					,@MaNgheNghiep
					,@DoiTuongId
					,@NguyCoLayNhiemId
					,@MaXetNghiem
					,@MaDuyet
					,@TrangThaiId
					,@DeleteFlag
					)
				
				-- Get the identity value
				SET @BenhNhanId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.BenhNhanDangKy_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.BenhNhanDangKy_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.BenhNhanDangKy_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Updates a record in the BenhNhanDangKy table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.BenhNhanDangKy_Update
(

	@BenhNhanId int   ,

	@MaDonVi varchar (15)  ,

	@MaBenhNhan nvarchar (20)  ,

	@NgayDangKy datetime   ,

	@NgayTao datetime   ,

	@UserTao nvarchar (30)  ,

	@NgaySua datetime   ,

	@UserSua nvarchar (30)  ,

	@TenBenhNhan nvarchar (30)  ,

	@NgaySinh datetime   ,

	@GioiTinh int   ,

	@Cmnd nvarchar (15)  ,

	@NgayCapCmnd datetime   ,

	@NoiCapCmnd nvarchar (50)  ,

	@ThTruDuong nvarchar (50)  ,

	@ThTruPhuong int   ,

	@ThTruQuan int   ,

	@ThTruTp int   ,

	@TmTruDuong nvarchar (50)  ,

	@TmTruPhuong int   ,

	@TmTruQuan int   ,

	@TmTruTp int   ,

	@DienThoai nvarchar (15)  ,

	@Email nvarchar (255)  ,

	@HinhAnh image   ,

	@VanTay nvarchar (256)  ,

	@GhiChu nvarchar (255)  ,

	@NgayDuyet datetime   ,

	@UserDuyet nvarchar (30)  ,

	@GhiChuDuyet nvarchar (255)  ,

	@NgayDangKyHiv datetime   ,

	@MaNgheNghiep char (3)  ,

	@DoiTuongId int   ,

	@NguyCoLayNhiemId int   ,

	@MaXetNghiem nvarchar (20)  ,

	@MaDuyet varchar (2)  ,

	@TrangThaiId int   ,

	@DeleteFlag bit   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[BenhNhanDangKy]
				SET
					[MaDonVi] = @MaDonVi
					,[MaBenhNhan] = @MaBenhNhan
					,[NgayDangKy] = @NgayDangKy
					,[NgayTao] = @NgayTao
					,[UserTao] = @UserTao
					,[NgaySua] = @NgaySua
					,[UserSua] = @UserSua
					,[TenBenhNhan] = @TenBenhNhan
					,[NgaySinh] = @NgaySinh
					,[GioiTinh] = @GioiTinh
					,[Cmnd] = @Cmnd
					,[NgayCapCmnd] = @NgayCapCmnd
					,[NoiCapCmnd] = @NoiCapCmnd
					,[ThTruDuong] = @ThTruDuong
					,[ThTruPhuong] = @ThTruPhuong
					,[ThTruQuan] = @ThTruQuan
					,[ThTruTp] = @ThTruTp
					,[TmTruDuong] = @TmTruDuong
					,[TmTruPhuong] = @TmTruPhuong
					,[TmTruQuan] = @TmTruQuan
					,[TmTruTp] = @TmTruTp
					,[DienThoai] = @DienThoai
					,[Email] = @Email
					,[HinhAnh] = @HinhAnh
					,[VanTay] = @VanTay
					,[GhiChu] = @GhiChu
					,[NgayDuyet] = @NgayDuyet
					,[UserDuyet] = @UserDuyet
					,[GhiChuDuyet] = @GhiChuDuyet
					,[NgayDangKyHiv] = @NgayDangKyHiv
					,[MaNgheNghiep] = @MaNgheNghiep
					,[DoiTuongId] = @DoiTuongId
					,[NguyCoLayNhiemId] = @NguyCoLayNhiemId
					,[MaXetNghiem] = @MaXetNghiem
					,[MaDuyet] = @MaDuyet
					,[TrangThaiId] = @TrangThaiId
					,[DeleteFlag] = @DeleteFlag
				WHERE
[BenhNhanId] = @BenhNhanId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.BenhNhanDangKy_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.BenhNhanDangKy_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.BenhNhanDangKy_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Deletes a record in the BenhNhanDangKy table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.BenhNhanDangKy_Delete
(

	@BenhNhanId int   
)
AS


				DELETE FROM [dbo].[BenhNhanDangKy] WITH (ROWLOCK) 
				WHERE
					[BenhNhanId] = @BenhNhanId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.BenhNhanDangKy_GetByTrangThaiId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.BenhNhanDangKy_GetByTrangThaiId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.BenhNhanDangKy_GetByTrangThaiId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Select records from the BenhNhanDangKy table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.BenhNhanDangKy_GetByTrangThaiId
(

	@TrangThaiId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[BenhNhanId],
					[MaDonVi],
					[MaBenhNhan],
					[NgayDangKy],
					[NgayTao],
					[UserTao],
					[NgaySua],
					[UserSua],
					[TenBenhNhan],
					[NgaySinh],
					[GioiTinh],
					[Cmnd],
					[NgayCapCmnd],
					[NoiCapCmnd],
					[ThTruDuong],
					[ThTruPhuong],
					[ThTruQuan],
					[ThTruTp],
					[TmTruDuong],
					[TmTruPhuong],
					[TmTruQuan],
					[TmTruTp],
					[DienThoai],
					[Email],
					[HinhAnh],
					[VanTay],
					[GhiChu],
					[NgayDuyet],
					[UserDuyet],
					[GhiChuDuyet],
					[NgayDangKyHiv],
					[MaNgheNghiep],
					[DoiTuongId],
					[NguyCoLayNhiemId],
					[MaXetNghiem],
					[MaDuyet],
					[TrangThaiId],
					[DeleteFlag]
				FROM
					[dbo].[BenhNhanDangKy]
				WHERE
					[TrangThaiId] = @TrangThaiId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.BenhNhanDangKy_GetByBenhNhanId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.BenhNhanDangKy_GetByBenhNhanId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.BenhNhanDangKy_GetByBenhNhanId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Select records from the BenhNhanDangKy table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.BenhNhanDangKy_GetByBenhNhanId
(

	@BenhNhanId int   
)
AS


				SELECT
					[BenhNhanId],
					[MaDonVi],
					[MaBenhNhan],
					[NgayDangKy],
					[NgayTao],
					[UserTao],
					[NgaySua],
					[UserSua],
					[TenBenhNhan],
					[NgaySinh],
					[GioiTinh],
					[Cmnd],
					[NgayCapCmnd],
					[NoiCapCmnd],
					[ThTruDuong],
					[ThTruPhuong],
					[ThTruQuan],
					[ThTruTp],
					[TmTruDuong],
					[TmTruPhuong],
					[TmTruQuan],
					[TmTruTp],
					[DienThoai],
					[Email],
					[HinhAnh],
					[VanTay],
					[GhiChu],
					[NgayDuyet],
					[UserDuyet],
					[GhiChuDuyet],
					[NgayDangKyHiv],
					[MaNgheNghiep],
					[DoiTuongId],
					[NguyCoLayNhiemId],
					[MaXetNghiem],
					[MaDuyet],
					[TrangThaiId],
					[DeleteFlag]
				FROM
					[dbo].[BenhNhanDangKy]
				WHERE
					[BenhNhanId] = @BenhNhanId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.BenhNhanDangKy_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.BenhNhanDangKy_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.BenhNhanDangKy_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Finds records in the BenhNhanDangKy table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.BenhNhanDangKy_Find
(

	@SearchUsingOR bit   = null ,

	@BenhNhanId int   = null ,

	@MaDonVi varchar (15)  = null ,

	@MaBenhNhan nvarchar (20)  = null ,

	@NgayDangKy datetime   = null ,

	@NgayTao datetime   = null ,

	@UserTao nvarchar (30)  = null ,

	@NgaySua datetime   = null ,

	@UserSua nvarchar (30)  = null ,

	@TenBenhNhan nvarchar (30)  = null ,

	@NgaySinh datetime   = null ,

	@GioiTinh int   = null ,

	@Cmnd nvarchar (15)  = null ,

	@NgayCapCmnd datetime   = null ,

	@NoiCapCmnd nvarchar (50)  = null ,

	@ThTruDuong nvarchar (50)  = null ,

	@ThTruPhuong int   = null ,

	@ThTruQuan int   = null ,

	@ThTruTp int   = null ,

	@TmTruDuong nvarchar (50)  = null ,

	@TmTruPhuong int   = null ,

	@TmTruQuan int   = null ,

	@TmTruTp int   = null ,

	@DienThoai nvarchar (15)  = null ,

	@Email nvarchar (255)  = null ,

	@HinhAnh image   = null ,

	@VanTay nvarchar (256)  = null ,

	@GhiChu nvarchar (255)  = null ,

	@NgayDuyet datetime   = null ,

	@UserDuyet nvarchar (30)  = null ,

	@GhiChuDuyet nvarchar (255)  = null ,

	@NgayDangKyHiv datetime   = null ,

	@MaNgheNghiep char (3)  = null ,

	@DoiTuongId int   = null ,

	@NguyCoLayNhiemId int   = null ,

	@MaXetNghiem nvarchar (20)  = null ,

	@MaDuyet varchar (2)  = null ,

	@TrangThaiId int   = null ,

	@DeleteFlag bit   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [BenhNhanId]
	, [MaDonVi]
	, [MaBenhNhan]
	, [NgayDangKy]
	, [NgayTao]
	, [UserTao]
	, [NgaySua]
	, [UserSua]
	, [TenBenhNhan]
	, [NgaySinh]
	, [GioiTinh]
	, [Cmnd]
	, [NgayCapCmnd]
	, [NoiCapCmnd]
	, [ThTruDuong]
	, [ThTruPhuong]
	, [ThTruQuan]
	, [ThTruTp]
	, [TmTruDuong]
	, [TmTruPhuong]
	, [TmTruQuan]
	, [TmTruTp]
	, [DienThoai]
	, [Email]
	, [HinhAnh]
	, [VanTay]
	, [GhiChu]
	, [NgayDuyet]
	, [UserDuyet]
	, [GhiChuDuyet]
	, [NgayDangKyHiv]
	, [MaNgheNghiep]
	, [DoiTuongId]
	, [NguyCoLayNhiemId]
	, [MaXetNghiem]
	, [MaDuyet]
	, [TrangThaiId]
	, [DeleteFlag]
    FROM
	[dbo].[BenhNhanDangKy]
    WHERE 
	 ([BenhNhanId] = @BenhNhanId OR @BenhNhanId IS NULL)
	AND ([MaDonVi] = @MaDonVi OR @MaDonVi IS NULL)
	AND ([MaBenhNhan] = @MaBenhNhan OR @MaBenhNhan IS NULL)
	AND ([NgayDangKy] = @NgayDangKy OR @NgayDangKy IS NULL)
	AND ([NgayTao] = @NgayTao OR @NgayTao IS NULL)
	AND ([UserTao] = @UserTao OR @UserTao IS NULL)
	AND ([NgaySua] = @NgaySua OR @NgaySua IS NULL)
	AND ([UserSua] = @UserSua OR @UserSua IS NULL)
	AND ([TenBenhNhan] = @TenBenhNhan OR @TenBenhNhan IS NULL)
	AND ([NgaySinh] = @NgaySinh OR @NgaySinh IS NULL)
	AND ([GioiTinh] = @GioiTinh OR @GioiTinh IS NULL)
	AND ([Cmnd] = @Cmnd OR @Cmnd IS NULL)
	AND ([NgayCapCmnd] = @NgayCapCmnd OR @NgayCapCmnd IS NULL)
	AND ([NoiCapCmnd] = @NoiCapCmnd OR @NoiCapCmnd IS NULL)
	AND ([ThTruDuong] = @ThTruDuong OR @ThTruDuong IS NULL)
	AND ([ThTruPhuong] = @ThTruPhuong OR @ThTruPhuong IS NULL)
	AND ([ThTruQuan] = @ThTruQuan OR @ThTruQuan IS NULL)
	AND ([ThTruTp] = @ThTruTp OR @ThTruTp IS NULL)
	AND ([TmTruDuong] = @TmTruDuong OR @TmTruDuong IS NULL)
	AND ([TmTruPhuong] = @TmTruPhuong OR @TmTruPhuong IS NULL)
	AND ([TmTruQuan] = @TmTruQuan OR @TmTruQuan IS NULL)
	AND ([TmTruTp] = @TmTruTp OR @TmTruTp IS NULL)
	AND ([DienThoai] = @DienThoai OR @DienThoai IS NULL)
	AND ([Email] = @Email OR @Email IS NULL)
	AND ([VanTay] = @VanTay OR @VanTay IS NULL)
	AND ([GhiChu] = @GhiChu OR @GhiChu IS NULL)
	AND ([NgayDuyet] = @NgayDuyet OR @NgayDuyet IS NULL)
	AND ([UserDuyet] = @UserDuyet OR @UserDuyet IS NULL)
	AND ([GhiChuDuyet] = @GhiChuDuyet OR @GhiChuDuyet IS NULL)
	AND ([NgayDangKyHiv] = @NgayDangKyHiv OR @NgayDangKyHiv IS NULL)
	AND ([MaNgheNghiep] = @MaNgheNghiep OR @MaNgheNghiep IS NULL)
	AND ([DoiTuongId] = @DoiTuongId OR @DoiTuongId IS NULL)
	AND ([NguyCoLayNhiemId] = @NguyCoLayNhiemId OR @NguyCoLayNhiemId IS NULL)
	AND ([MaXetNghiem] = @MaXetNghiem OR @MaXetNghiem IS NULL)
	AND ([MaDuyet] = @MaDuyet OR @MaDuyet IS NULL)
	AND ([TrangThaiId] = @TrangThaiId OR @TrangThaiId IS NULL)
	AND ([DeleteFlag] = @DeleteFlag OR @DeleteFlag IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [BenhNhanId]
	, [MaDonVi]
	, [MaBenhNhan]
	, [NgayDangKy]
	, [NgayTao]
	, [UserTao]
	, [NgaySua]
	, [UserSua]
	, [TenBenhNhan]
	, [NgaySinh]
	, [GioiTinh]
	, [Cmnd]
	, [NgayCapCmnd]
	, [NoiCapCmnd]
	, [ThTruDuong]
	, [ThTruPhuong]
	, [ThTruQuan]
	, [ThTruTp]
	, [TmTruDuong]
	, [TmTruPhuong]
	, [TmTruQuan]
	, [TmTruTp]
	, [DienThoai]
	, [Email]
	, [HinhAnh]
	, [VanTay]
	, [GhiChu]
	, [NgayDuyet]
	, [UserDuyet]
	, [GhiChuDuyet]
	, [NgayDangKyHiv]
	, [MaNgheNghiep]
	, [DoiTuongId]
	, [NguyCoLayNhiemId]
	, [MaXetNghiem]
	, [MaDuyet]
	, [TrangThaiId]
	, [DeleteFlag]
    FROM
	[dbo].[BenhNhanDangKy]
    WHERE 
	 ([BenhNhanId] = @BenhNhanId AND @BenhNhanId is not null)
	OR ([MaDonVi] = @MaDonVi AND @MaDonVi is not null)
	OR ([MaBenhNhan] = @MaBenhNhan AND @MaBenhNhan is not null)
	OR ([NgayDangKy] = @NgayDangKy AND @NgayDangKy is not null)
	OR ([NgayTao] = @NgayTao AND @NgayTao is not null)
	OR ([UserTao] = @UserTao AND @UserTao is not null)
	OR ([NgaySua] = @NgaySua AND @NgaySua is not null)
	OR ([UserSua] = @UserSua AND @UserSua is not null)
	OR ([TenBenhNhan] = @TenBenhNhan AND @TenBenhNhan is not null)
	OR ([NgaySinh] = @NgaySinh AND @NgaySinh is not null)
	OR ([GioiTinh] = @GioiTinh AND @GioiTinh is not null)
	OR ([Cmnd] = @Cmnd AND @Cmnd is not null)
	OR ([NgayCapCmnd] = @NgayCapCmnd AND @NgayCapCmnd is not null)
	OR ([NoiCapCmnd] = @NoiCapCmnd AND @NoiCapCmnd is not null)
	OR ([ThTruDuong] = @ThTruDuong AND @ThTruDuong is not null)
	OR ([ThTruPhuong] = @ThTruPhuong AND @ThTruPhuong is not null)
	OR ([ThTruQuan] = @ThTruQuan AND @ThTruQuan is not null)
	OR ([ThTruTp] = @ThTruTp AND @ThTruTp is not null)
	OR ([TmTruDuong] = @TmTruDuong AND @TmTruDuong is not null)
	OR ([TmTruPhuong] = @TmTruPhuong AND @TmTruPhuong is not null)
	OR ([TmTruQuan] = @TmTruQuan AND @TmTruQuan is not null)
	OR ([TmTruTp] = @TmTruTp AND @TmTruTp is not null)
	OR ([DienThoai] = @DienThoai AND @DienThoai is not null)
	OR ([Email] = @Email AND @Email is not null)
	OR ([VanTay] = @VanTay AND @VanTay is not null)
	OR ([GhiChu] = @GhiChu AND @GhiChu is not null)
	OR ([NgayDuyet] = @NgayDuyet AND @NgayDuyet is not null)
	OR ([UserDuyet] = @UserDuyet AND @UserDuyet is not null)
	OR ([GhiChuDuyet] = @GhiChuDuyet AND @GhiChuDuyet is not null)
	OR ([NgayDangKyHiv] = @NgayDangKyHiv AND @NgayDangKyHiv is not null)
	OR ([MaNgheNghiep] = @MaNgheNghiep AND @MaNgheNghiep is not null)
	OR ([DoiTuongId] = @DoiTuongId AND @DoiTuongId is not null)
	OR ([NguyCoLayNhiemId] = @NguyCoLayNhiemId AND @NguyCoLayNhiemId is not null)
	OR ([MaXetNghiem] = @MaXetNghiem AND @MaXetNghiem is not null)
	OR ([MaDuyet] = @MaDuyet AND @MaDuyet is not null)
	OR ([TrangThaiId] = @TrangThaiId AND @TrangThaiId is not null)
	OR ([DeleteFlag] = @DeleteFlag AND @DeleteFlag is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.s_Users_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.s_Users_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.s_Users_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets all records from the s_Users table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.s_Users_Get_List

AS


				
				SELECT
					[Id],
					[UserName],
					[FullName],
					[Password],
					[salt],
					[Office],
					[Deleted]
				FROM
					[dbo].[s_Users]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.s_Users_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.s_Users_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.s_Users_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets records from the s_Users table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.s_Users_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Id])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [Id]'
				SET @SQL = @SQL + ' FROM [dbo].[s_Users]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[Id], O.[UserName], O.[FullName], O.[Password], O.[salt], O.[Office], O.[Deleted]
				FROM
				    [dbo].[s_Users] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Id] = PageIndex.[Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[s_Users]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.s_Users_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.s_Users_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.s_Users_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Inserts a record into the s_Users table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.s_Users_Insert
(

	@Id int    OUTPUT,

	@UserName nvarchar (255)  ,

	@FullName nvarchar (50)  ,

	@Password varchar (64)  ,

	@Salt varchar (10)  ,

	@Office nvarchar (50)  ,

	@Deleted bit   
)
AS


				
				INSERT INTO [dbo].[s_Users]
					(
					[UserName]
					,[FullName]
					,[Password]
					,[salt]
					,[Office]
					,[Deleted]
					)
				VALUES
					(
					@UserName
					,@FullName
					,@Password
					,@Salt
					,@Office
					,@Deleted
					)
				
				-- Get the identity value
				SET @Id = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.s_Users_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.s_Users_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.s_Users_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Updates a record in the s_Users table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.s_Users_Update
(

	@Id int   ,

	@UserName nvarchar (255)  ,

	@FullName nvarchar (50)  ,

	@Password varchar (64)  ,

	@Salt varchar (10)  ,

	@Office nvarchar (50)  ,

	@Deleted bit   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[s_Users]
				SET
					[UserName] = @UserName
					,[FullName] = @FullName
					,[Password] = @Password
					,[salt] = @Salt
					,[Office] = @Office
					,[Deleted] = @Deleted
				WHERE
[Id] = @Id 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.s_Users_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.s_Users_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.s_Users_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Deletes a record in the s_Users table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.s_Users_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[s_Users] WITH (ROWLOCK) 
				WHERE
					[Id] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.s_Users_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.s_Users_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.s_Users_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Select records from the s_Users table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.s_Users_GetById
(

	@Id int   
)
AS


				SELECT
					[Id],
					[UserName],
					[FullName],
					[Password],
					[salt],
					[Office],
					[Deleted]
				FROM
					[dbo].[s_Users]
				WHERE
					[Id] = @Id
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.s_Users_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.s_Users_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.s_Users_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Finds records in the s_Users table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.s_Users_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@UserName nvarchar (255)  = null ,

	@FullName nvarchar (50)  = null ,

	@Password varchar (64)  = null ,

	@Salt varchar (10)  = null ,

	@Office nvarchar (50)  = null ,

	@Deleted bit   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Id]
	, [UserName]
	, [FullName]
	, [Password]
	, [salt]
	, [Office]
	, [Deleted]
    FROM
	[dbo].[s_Users]
    WHERE 
	 ([Id] = @Id OR @Id IS NULL)
	AND ([UserName] = @UserName OR @UserName IS NULL)
	AND ([FullName] = @FullName OR @FullName IS NULL)
	AND ([Password] = @Password OR @Password IS NULL)
	AND ([salt] = @Salt OR @Salt IS NULL)
	AND ([Office] = @Office OR @Office IS NULL)
	AND ([Deleted] = @Deleted OR @Deleted IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Id]
	, [UserName]
	, [FullName]
	, [Password]
	, [salt]
	, [Office]
	, [Deleted]
    FROM
	[dbo].[s_Users]
    WHERE 
	 ([Id] = @Id AND @Id is not null)
	OR ([UserName] = @UserName AND @UserName is not null)
	OR ([FullName] = @FullName AND @FullName is not null)
	OR ([Password] = @Password AND @Password is not null)
	OR ([salt] = @Salt AND @Salt is not null)
	OR ([Office] = @Office AND @Office is not null)
	OR ([Deleted] = @Deleted AND @Deleted is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThayDoiPhacDo_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThayDoiPhacDo_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThayDoiPhacDo_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets all records from the ThayDoiPhacDo table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThayDoiPhacDo_Get_List

AS


				
				SELECT
					[Id],
					[BenhNhanId],
					[MaPhacDo],
					[NgayThayDoi],
					[UserName],
					[LyDo]
				FROM
					[dbo].[ThayDoiPhacDo]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThayDoiPhacDo_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThayDoiPhacDo_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThayDoiPhacDo_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets records from the ThayDoiPhacDo table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThayDoiPhacDo_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Id])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [Id]'
				SET @SQL = @SQL + ' FROM [dbo].[ThayDoiPhacDo]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[Id], O.[BenhNhanId], O.[MaPhacDo], O.[NgayThayDoi], O.[UserName], O.[LyDo]
				FROM
				    [dbo].[ThayDoiPhacDo] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Id] = PageIndex.[Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[ThayDoiPhacDo]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThayDoiPhacDo_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThayDoiPhacDo_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThayDoiPhacDo_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Inserts a record into the ThayDoiPhacDo table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThayDoiPhacDo_Insert
(

	@Id int    OUTPUT,

	@BenhNhanId int   ,

	@MaPhacDo char (2)  ,

	@NgayThayDoi datetime   ,

	@UserName nvarchar (30)  ,

	@LyDo nvarchar (255)  
)
AS


				
				INSERT INTO [dbo].[ThayDoiPhacDo]
					(
					[BenhNhanId]
					,[MaPhacDo]
					,[NgayThayDoi]
					,[UserName]
					,[LyDo]
					)
				VALUES
					(
					@BenhNhanId
					,@MaPhacDo
					,@NgayThayDoi
					,@UserName
					,@LyDo
					)
				
				-- Get the identity value
				SET @Id = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThayDoiPhacDo_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThayDoiPhacDo_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThayDoiPhacDo_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Updates a record in the ThayDoiPhacDo table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThayDoiPhacDo_Update
(

	@Id int   ,

	@BenhNhanId int   ,

	@MaPhacDo char (2)  ,

	@NgayThayDoi datetime   ,

	@UserName nvarchar (30)  ,

	@LyDo nvarchar (255)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[ThayDoiPhacDo]
				SET
					[BenhNhanId] = @BenhNhanId
					,[MaPhacDo] = @MaPhacDo
					,[NgayThayDoi] = @NgayThayDoi
					,[UserName] = @UserName
					,[LyDo] = @LyDo
				WHERE
[Id] = @Id 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThayDoiPhacDo_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThayDoiPhacDo_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThayDoiPhacDo_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Deletes a record in the ThayDoiPhacDo table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThayDoiPhacDo_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[ThayDoiPhacDo] WITH (ROWLOCK) 
				WHERE
					[Id] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThayDoiPhacDo_GetByBenhNhanId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThayDoiPhacDo_GetByBenhNhanId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThayDoiPhacDo_GetByBenhNhanId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Select records from the ThayDoiPhacDo table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThayDoiPhacDo_GetByBenhNhanId
(

	@BenhNhanId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[Id],
					[BenhNhanId],
					[MaPhacDo],
					[NgayThayDoi],
					[UserName],
					[LyDo]
				FROM
					[dbo].[ThayDoiPhacDo]
				WHERE
					[BenhNhanId] = @BenhNhanId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThayDoiPhacDo_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThayDoiPhacDo_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThayDoiPhacDo_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Select records from the ThayDoiPhacDo table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThayDoiPhacDo_GetById
(

	@Id int   
)
AS


				SELECT
					[Id],
					[BenhNhanId],
					[MaPhacDo],
					[NgayThayDoi],
					[UserName],
					[LyDo]
				FROM
					[dbo].[ThayDoiPhacDo]
				WHERE
					[Id] = @Id
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThayDoiPhacDo_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThayDoiPhacDo_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThayDoiPhacDo_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Finds records in the ThayDoiPhacDo table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThayDoiPhacDo_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@BenhNhanId int   = null ,

	@MaPhacDo char (2)  = null ,

	@NgayThayDoi datetime   = null ,

	@UserName nvarchar (30)  = null ,

	@LyDo nvarchar (255)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Id]
	, [BenhNhanId]
	, [MaPhacDo]
	, [NgayThayDoi]
	, [UserName]
	, [LyDo]
    FROM
	[dbo].[ThayDoiPhacDo]
    WHERE 
	 ([Id] = @Id OR @Id IS NULL)
	AND ([BenhNhanId] = @BenhNhanId OR @BenhNhanId IS NULL)
	AND ([MaPhacDo] = @MaPhacDo OR @MaPhacDo IS NULL)
	AND ([NgayThayDoi] = @NgayThayDoi OR @NgayThayDoi IS NULL)
	AND ([UserName] = @UserName OR @UserName IS NULL)
	AND ([LyDo] = @LyDo OR @LyDo IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Id]
	, [BenhNhanId]
	, [MaPhacDo]
	, [NgayThayDoi]
	, [UserName]
	, [LyDo]
    FROM
	[dbo].[ThayDoiPhacDo]
    WHERE 
	 ([Id] = @Id AND @Id is not null)
	OR ([BenhNhanId] = @BenhNhanId AND @BenhNhanId is not null)
	OR ([MaPhacDo] = @MaPhacDo AND @MaPhacDo is not null)
	OR ([NgayThayDoi] = @NgayThayDoi AND @NgayThayDoi is not null)
	OR ([UserName] = @UserName AND @UserName is not null)
	OR ([LyDo] = @LyDo AND @LyDo is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThayDoiDieuTri_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThayDoiDieuTri_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThayDoiDieuTri_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets all records from the ThayDoiDieuTri table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThayDoiDieuTri_Get_List

AS


				
				SELECT
					[Id],
					[BenhNhanId],
					[UserName],
					[NgayThayDoi],
					[LyDo]
				FROM
					[dbo].[ThayDoiDieuTri]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThayDoiDieuTri_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThayDoiDieuTri_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThayDoiDieuTri_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets records from the ThayDoiDieuTri table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThayDoiDieuTri_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Id])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [Id]'
				SET @SQL = @SQL + ' FROM [dbo].[ThayDoiDieuTri]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[Id], O.[BenhNhanId], O.[UserName], O.[NgayThayDoi], O.[LyDo]
				FROM
				    [dbo].[ThayDoiDieuTri] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Id] = PageIndex.[Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[ThayDoiDieuTri]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThayDoiDieuTri_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThayDoiDieuTri_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThayDoiDieuTri_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Inserts a record into the ThayDoiDieuTri table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThayDoiDieuTri_Insert
(

	@Id int    OUTPUT,

	@BenhNhanId int   ,

	@UserName nvarchar (30)  ,

	@NgayThayDoi datetime   ,

	@LyDo nvarchar (200)  
)
AS


				
				INSERT INTO [dbo].[ThayDoiDieuTri]
					(
					[BenhNhanId]
					,[UserName]
					,[NgayThayDoi]
					,[LyDo]
					)
				VALUES
					(
					@BenhNhanId
					,@UserName
					,@NgayThayDoi
					,@LyDo
					)
				
				-- Get the identity value
				SET @Id = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThayDoiDieuTri_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThayDoiDieuTri_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThayDoiDieuTri_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Updates a record in the ThayDoiDieuTri table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThayDoiDieuTri_Update
(

	@Id int   ,

	@BenhNhanId int   ,

	@UserName nvarchar (30)  ,

	@NgayThayDoi datetime   ,

	@LyDo nvarchar (200)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[ThayDoiDieuTri]
				SET
					[BenhNhanId] = @BenhNhanId
					,[UserName] = @UserName
					,[NgayThayDoi] = @NgayThayDoi
					,[LyDo] = @LyDo
				WHERE
[Id] = @Id 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThayDoiDieuTri_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThayDoiDieuTri_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThayDoiDieuTri_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Deletes a record in the ThayDoiDieuTri table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThayDoiDieuTri_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[ThayDoiDieuTri] WITH (ROWLOCK) 
				WHERE
					[Id] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThayDoiDieuTri_GetByBenhNhanId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThayDoiDieuTri_GetByBenhNhanId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThayDoiDieuTri_GetByBenhNhanId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Select records from the ThayDoiDieuTri table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThayDoiDieuTri_GetByBenhNhanId
(

	@BenhNhanId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[Id],
					[BenhNhanId],
					[UserName],
					[NgayThayDoi],
					[LyDo]
				FROM
					[dbo].[ThayDoiDieuTri]
				WHERE
					[BenhNhanId] = @BenhNhanId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThayDoiDieuTri_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThayDoiDieuTri_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThayDoiDieuTri_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Select records from the ThayDoiDieuTri table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThayDoiDieuTri_GetById
(

	@Id int   
)
AS


				SELECT
					[Id],
					[BenhNhanId],
					[UserName],
					[NgayThayDoi],
					[LyDo]
				FROM
					[dbo].[ThayDoiDieuTri]
				WHERE
					[Id] = @Id
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ThayDoiDieuTri_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ThayDoiDieuTri_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ThayDoiDieuTri_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Finds records in the ThayDoiDieuTri table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ThayDoiDieuTri_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@BenhNhanId int   = null ,

	@UserName nvarchar (30)  = null ,

	@NgayThayDoi datetime   = null ,

	@LyDo nvarchar (200)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Id]
	, [BenhNhanId]
	, [UserName]
	, [NgayThayDoi]
	, [LyDo]
    FROM
	[dbo].[ThayDoiDieuTri]
    WHERE 
	 ([Id] = @Id OR @Id IS NULL)
	AND ([BenhNhanId] = @BenhNhanId OR @BenhNhanId IS NULL)
	AND ([UserName] = @UserName OR @UserName IS NULL)
	AND ([NgayThayDoi] = @NgayThayDoi OR @NgayThayDoi IS NULL)
	AND ([LyDo] = @LyDo OR @LyDo IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Id]
	, [BenhNhanId]
	, [UserName]
	, [NgayThayDoi]
	, [LyDo]
    FROM
	[dbo].[ThayDoiDieuTri]
    WHERE 
	 ([Id] = @Id AND @Id is not null)
	OR ([BenhNhanId] = @BenhNhanId AND @BenhNhanId is not null)
	OR ([UserName] = @UserName AND @UserName is not null)
	OR ([NgayThayDoi] = @NgayThayDoi AND @NgayThayDoi is not null)
	OR ([LyDo] = @LyDo AND @LyDo is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_phacdo_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_phacdo_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_phacdo_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets all records from the ut_phacdo table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_phacdo_Get_List

AS


				
				SELECT
					[Id],
					[f_phacdo],
					[f_diengiai],
					[f_nhom]
				FROM
					[dbo].[ut_phacdo]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_phacdo_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_phacdo_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_phacdo_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets records from the ut_phacdo table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_phacdo_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Id])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [Id]'
				SET @SQL = @SQL + ' FROM [dbo].[ut_phacdo]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[Id], O.[f_phacdo], O.[f_diengiai], O.[f_nhom]
				FROM
				    [dbo].[ut_phacdo] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Id] = PageIndex.[Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[ut_phacdo]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_phacdo_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_phacdo_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_phacdo_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Inserts a record into the ut_phacdo table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_phacdo_Insert
(

	@Id int    OUTPUT,

	@FPhacdo nvarchar (2)  ,

	@FDiengiai nvarchar (100)  ,

	@FNhom tinyint   
)
AS


				
				INSERT INTO [dbo].[ut_phacdo]
					(
					[f_phacdo]
					,[f_diengiai]
					,[f_nhom]
					)
				VALUES
					(
					@FPhacdo
					,@FDiengiai
					,@FNhom
					)
				
				-- Get the identity value
				SET @Id = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_phacdo_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_phacdo_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_phacdo_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Updates a record in the ut_phacdo table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_phacdo_Update
(

	@Id int   ,

	@FPhacdo nvarchar (2)  ,

	@FDiengiai nvarchar (100)  ,

	@FNhom tinyint   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[ut_phacdo]
				SET
					[f_phacdo] = @FPhacdo
					,[f_diengiai] = @FDiengiai
					,[f_nhom] = @FNhom
				WHERE
[Id] = @Id 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_phacdo_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_phacdo_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_phacdo_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Deletes a record in the ut_phacdo table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_phacdo_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[ut_phacdo] WITH (ROWLOCK) 
				WHERE
					[Id] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_phacdo_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_phacdo_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_phacdo_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Select records from the ut_phacdo table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_phacdo_GetById
(

	@Id int   
)
AS


				SELECT
					[Id],
					[f_phacdo],
					[f_diengiai],
					[f_nhom]
				FROM
					[dbo].[ut_phacdo]
				WHERE
					[Id] = @Id
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_phacdo_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_phacdo_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_phacdo_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Finds records in the ut_phacdo table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_phacdo_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@FPhacdo nvarchar (2)  = null ,

	@FDiengiai nvarchar (100)  = null ,

	@FNhom tinyint   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Id]
	, [f_phacdo]
	, [f_diengiai]
	, [f_nhom]
    FROM
	[dbo].[ut_phacdo]
    WHERE 
	 ([Id] = @Id OR @Id IS NULL)
	AND ([f_phacdo] = @FPhacdo OR @FPhacdo IS NULL)
	AND ([f_diengiai] = @FDiengiai OR @FDiengiai IS NULL)
	AND ([f_nhom] = @FNhom OR @FNhom IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Id]
	, [f_phacdo]
	, [f_diengiai]
	, [f_nhom]
    FROM
	[dbo].[ut_phacdo]
    WHERE 
	 ([Id] = @Id AND @Id is not null)
	OR ([f_phacdo] = @FPhacdo AND @FPhacdo is not null)
	OR ([f_diengiai] = @FDiengiai AND @FDiengiai is not null)
	OR ([f_nhom] = @FNhom AND @FNhom is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_dmdv_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_dmdv_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_dmdv_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets all records from the ut_dmdv table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_dmdv_Get_List

AS


				
				SELECT
					[Id],
					[f_madv],
					[f_tendv],
					[f_dt1],
					[f_dt2]
				FROM
					[dbo].[ut_dmdv]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_dmdv_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_dmdv_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_dmdv_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets records from the ut_dmdv table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_dmdv_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Id])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [Id]'
				SET @SQL = @SQL + ' FROM [dbo].[ut_dmdv]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[Id], O.[f_madv], O.[f_tendv], O.[f_dt1], O.[f_dt2]
				FROM
				    [dbo].[ut_dmdv] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Id] = PageIndex.[Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[ut_dmdv]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_dmdv_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_dmdv_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_dmdv_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Inserts a record into the ut_dmdv table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_dmdv_Insert
(

	@Id int    OUTPUT,

	@FMadv nvarchar (15)  ,

	@FTendv nvarchar (50)  ,

	@FDt1 bit   ,

	@FDt2 bit   
)
AS


				
				INSERT INTO [dbo].[ut_dmdv]
					(
					[f_madv]
					,[f_tendv]
					,[f_dt1]
					,[f_dt2]
					)
				VALUES
					(
					@FMadv
					,@FTendv
					,@FDt1
					,@FDt2
					)
				
				-- Get the identity value
				SET @Id = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_dmdv_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_dmdv_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_dmdv_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Updates a record in the ut_dmdv table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_dmdv_Update
(

	@Id int   ,

	@FMadv nvarchar (15)  ,

	@FTendv nvarchar (50)  ,

	@FDt1 bit   ,

	@FDt2 bit   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[ut_dmdv]
				SET
					[f_madv] = @FMadv
					,[f_tendv] = @FTendv
					,[f_dt1] = @FDt1
					,[f_dt2] = @FDt2
				WHERE
[Id] = @Id 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_dmdv_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_dmdv_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_dmdv_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Deletes a record in the ut_dmdv table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_dmdv_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[ut_dmdv] WITH (ROWLOCK) 
				WHERE
					[Id] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_dmdv_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_dmdv_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_dmdv_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Select records from the ut_dmdv table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_dmdv_GetById
(

	@Id int   
)
AS


				SELECT
					[Id],
					[f_madv],
					[f_tendv],
					[f_dt1],
					[f_dt2]
				FROM
					[dbo].[ut_dmdv]
				WHERE
					[Id] = @Id
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_dmdv_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_dmdv_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_dmdv_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Finds records in the ut_dmdv table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_dmdv_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@FMadv nvarchar (15)  = null ,

	@FTendv nvarchar (50)  = null ,

	@FDt1 bit   = null ,

	@FDt2 bit   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Id]
	, [f_madv]
	, [f_tendv]
	, [f_dt1]
	, [f_dt2]
    FROM
	[dbo].[ut_dmdv]
    WHERE 
	 ([Id] = @Id OR @Id IS NULL)
	AND ([f_madv] = @FMadv OR @FMadv IS NULL)
	AND ([f_tendv] = @FTendv OR @FTendv IS NULL)
	AND ([f_dt1] = @FDt1 OR @FDt1 IS NULL)
	AND ([f_dt2] = @FDt2 OR @FDt2 IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Id]
	, [f_madv]
	, [f_tendv]
	, [f_dt1]
	, [f_dt2]
    FROM
	[dbo].[ut_dmdv]
    WHERE 
	 ([Id] = @Id AND @Id is not null)
	OR ([f_madv] = @FMadv AND @FMadv is not null)
	OR ([f_tendv] = @FTendv AND @FTendv is not null)
	OR ([f_dt1] = @FDt1 AND @FDt1 is not null)
	OR ([f_dt2] = @FDt2 AND @FDt2 is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_dmxn_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_dmxn_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_dmxn_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets all records from the ut_dmxn table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_dmxn_Get_List

AS


				
				SELECT
					[Id],
					[f_maxn],
					[f_tenxn]
				FROM
					[dbo].[ut_dmxn]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_dmxn_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_dmxn_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_dmxn_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets records from the ut_dmxn table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_dmxn_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Id])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [Id]'
				SET @SQL = @SQL + ' FROM [dbo].[ut_dmxn]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[Id], O.[f_maxn], O.[f_tenxn]
				FROM
				    [dbo].[ut_dmxn] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Id] = PageIndex.[Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[ut_dmxn]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_dmxn_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_dmxn_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_dmxn_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Inserts a record into the ut_dmxn table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_dmxn_Insert
(

	@Id int    OUTPUT,

	@FMaxn nvarchar (2)  ,

	@FTenxn nvarchar (50)  
)
AS


				
				INSERT INTO [dbo].[ut_dmxn]
					(
					[f_maxn]
					,[f_tenxn]
					)
				VALUES
					(
					@FMaxn
					,@FTenxn
					)
				
				-- Get the identity value
				SET @Id = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_dmxn_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_dmxn_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_dmxn_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Updates a record in the ut_dmxn table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_dmxn_Update
(

	@Id int   ,

	@FMaxn nvarchar (2)  ,

	@FTenxn nvarchar (50)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[ut_dmxn]
				SET
					[f_maxn] = @FMaxn
					,[f_tenxn] = @FTenxn
				WHERE
[Id] = @Id 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_dmxn_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_dmxn_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_dmxn_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Deletes a record in the ut_dmxn table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_dmxn_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[ut_dmxn] WITH (ROWLOCK) 
				WHERE
					[Id] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_dmxn_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_dmxn_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_dmxn_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Select records from the ut_dmxn table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_dmxn_GetById
(

	@Id int   
)
AS


				SELECT
					[Id],
					[f_maxn],
					[f_tenxn]
				FROM
					[dbo].[ut_dmxn]
				WHERE
					[Id] = @Id
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_dmxn_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_dmxn_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_dmxn_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Finds records in the ut_dmxn table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_dmxn_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@FMaxn nvarchar (2)  = null ,

	@FTenxn nvarchar (50)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Id]
	, [f_maxn]
	, [f_tenxn]
    FROM
	[dbo].[ut_dmxn]
    WHERE 
	 ([Id] = @Id OR @Id IS NULL)
	AND ([f_maxn] = @FMaxn OR @FMaxn IS NULL)
	AND ([f_tenxn] = @FTenxn OR @FTenxn IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Id]
	, [f_maxn]
	, [f_tenxn]
    FROM
	[dbo].[ut_dmxn]
    WHERE 
	 ([Id] = @Id AND @Id is not null)
	OR ([f_maxn] = @FMaxn AND @FMaxn is not null)
	OR ([f_tenxn] = @FTenxn AND @FTenxn is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.PhanLoaiBenhNhan_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.PhanLoaiBenhNhan_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.PhanLoaiBenhNhan_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets all records from the PhanLoaiBenhNhan table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.PhanLoaiBenhNhan_Get_List

AS


				
				SELECT
					[Id],
					[TenPhanLoai]
				FROM
					[dbo].[PhanLoaiBenhNhan]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.PhanLoaiBenhNhan_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.PhanLoaiBenhNhan_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.PhanLoaiBenhNhan_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets records from the PhanLoaiBenhNhan table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.PhanLoaiBenhNhan_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Id])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [Id]'
				SET @SQL = @SQL + ' FROM [dbo].[PhanLoaiBenhNhan]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[Id], O.[TenPhanLoai]
				FROM
				    [dbo].[PhanLoaiBenhNhan] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Id] = PageIndex.[Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[PhanLoaiBenhNhan]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.PhanLoaiBenhNhan_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.PhanLoaiBenhNhan_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.PhanLoaiBenhNhan_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Inserts a record into the PhanLoaiBenhNhan table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.PhanLoaiBenhNhan_Insert
(

	@Id int   ,

	@TenPhanLoai nvarchar (100)  
)
AS


				
				INSERT INTO [dbo].[PhanLoaiBenhNhan]
					(
					[Id]
					,[TenPhanLoai]
					)
				VALUES
					(
					@Id
					,@TenPhanLoai
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.PhanLoaiBenhNhan_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.PhanLoaiBenhNhan_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.PhanLoaiBenhNhan_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Updates a record in the PhanLoaiBenhNhan table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.PhanLoaiBenhNhan_Update
(

	@Id int   ,

	@OriginalId int   ,

	@TenPhanLoai nvarchar (100)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[PhanLoaiBenhNhan]
				SET
					[Id] = @Id
					,[TenPhanLoai] = @TenPhanLoai
				WHERE
[Id] = @OriginalId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.PhanLoaiBenhNhan_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.PhanLoaiBenhNhan_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.PhanLoaiBenhNhan_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Deletes a record in the PhanLoaiBenhNhan table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.PhanLoaiBenhNhan_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[PhanLoaiBenhNhan] WITH (ROWLOCK) 
				WHERE
					[Id] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.PhanLoaiBenhNhan_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.PhanLoaiBenhNhan_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.PhanLoaiBenhNhan_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Select records from the PhanLoaiBenhNhan table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.PhanLoaiBenhNhan_GetById
(

	@Id int   
)
AS


				SELECT
					[Id],
					[TenPhanLoai]
				FROM
					[dbo].[PhanLoaiBenhNhan]
				WHERE
					[Id] = @Id
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.PhanLoaiBenhNhan_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.PhanLoaiBenhNhan_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.PhanLoaiBenhNhan_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Finds records in the PhanLoaiBenhNhan table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.PhanLoaiBenhNhan_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@TenPhanLoai nvarchar (100)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Id]
	, [TenPhanLoai]
    FROM
	[dbo].[PhanLoaiBenhNhan]
    WHERE 
	 ([Id] = @Id OR @Id IS NULL)
	AND ([TenPhanLoai] = @TenPhanLoai OR @TenPhanLoai IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Id]
	, [TenPhanLoai]
    FROM
	[dbo].[PhanLoaiBenhNhan]
    WHERE 
	 ([Id] = @Id AND @Id is not null)
	OR ([TenPhanLoai] = @TenPhanLoai AND @TenPhanLoai is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_dmuser_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_dmuser_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_dmuser_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets all records from the ut_dmuser table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_dmuser_Get_List

AS


				
				SELECT
					[Id],
					[f_tendn],
					[f_madv],
					[f_tennd],
					[f_quyen],
					[f_xoa]
				FROM
					[dbo].[ut_dmuser]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_dmuser_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_dmuser_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_dmuser_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets records from the ut_dmuser table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_dmuser_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Id])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [Id]'
				SET @SQL = @SQL + ' FROM [dbo].[ut_dmuser]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[Id], O.[f_tendn], O.[f_madv], O.[f_tennd], O.[f_quyen], O.[f_xoa]
				FROM
				    [dbo].[ut_dmuser] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Id] = PageIndex.[Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[ut_dmuser]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_dmuser_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_dmuser_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_dmuser_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Inserts a record into the ut_dmuser table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_dmuser_Insert
(

	@Id int    OUTPUT,

	@FTendn nvarchar (30)  ,

	@FMadv nvarchar (15)  ,

	@FTennd nvarchar (50)  ,

	@FQuyen nvarchar (50)  ,

	@FXoa bit   
)
AS


				
				INSERT INTO [dbo].[ut_dmuser]
					(
					[f_tendn]
					,[f_madv]
					,[f_tennd]
					,[f_quyen]
					,[f_xoa]
					)
				VALUES
					(
					@FTendn
					,@FMadv
					,@FTennd
					,@FQuyen
					,@FXoa
					)
				
				-- Get the identity value
				SET @Id = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_dmuser_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_dmuser_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_dmuser_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Updates a record in the ut_dmuser table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_dmuser_Update
(

	@Id int   ,

	@FTendn nvarchar (30)  ,

	@FMadv nvarchar (15)  ,

	@FTennd nvarchar (50)  ,

	@FQuyen nvarchar (50)  ,

	@FXoa bit   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[ut_dmuser]
				SET
					[f_tendn] = @FTendn
					,[f_madv] = @FMadv
					,[f_tennd] = @FTennd
					,[f_quyen] = @FQuyen
					,[f_xoa] = @FXoa
				WHERE
[Id] = @Id 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_dmuser_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_dmuser_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_dmuser_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Deletes a record in the ut_dmuser table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_dmuser_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[ut_dmuser] WITH (ROWLOCK) 
				WHERE
					[Id] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_dmuser_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_dmuser_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_dmuser_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Select records from the ut_dmuser table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_dmuser_GetById
(

	@Id int   
)
AS


				SELECT
					[Id],
					[f_tendn],
					[f_madv],
					[f_tennd],
					[f_quyen],
					[f_xoa]
				FROM
					[dbo].[ut_dmuser]
				WHERE
					[Id] = @Id
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_dmuser_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_dmuser_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_dmuser_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Finds records in the ut_dmuser table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_dmuser_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@FTendn nvarchar (30)  = null ,

	@FMadv nvarchar (15)  = null ,

	@FTennd nvarchar (50)  = null ,

	@FQuyen nvarchar (50)  = null ,

	@FXoa bit   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Id]
	, [f_tendn]
	, [f_madv]
	, [f_tennd]
	, [f_quyen]
	, [f_xoa]
    FROM
	[dbo].[ut_dmuser]
    WHERE 
	 ([Id] = @Id OR @Id IS NULL)
	AND ([f_tendn] = @FTendn OR @FTendn IS NULL)
	AND ([f_madv] = @FMadv OR @FMadv IS NULL)
	AND ([f_tennd] = @FTennd OR @FTennd IS NULL)
	AND ([f_quyen] = @FQuyen OR @FQuyen IS NULL)
	AND ([f_xoa] = @FXoa OR @FXoa IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Id]
	, [f_tendn]
	, [f_madv]
	, [f_tennd]
	, [f_quyen]
	, [f_xoa]
    FROM
	[dbo].[ut_dmuser]
    WHERE 
	 ([Id] = @Id AND @Id is not null)
	OR ([f_tendn] = @FTendn AND @FTendn is not null)
	OR ([f_madv] = @FMadv AND @FMadv is not null)
	OR ([f_tennd] = @FTennd AND @FTennd is not null)
	OR ([f_quyen] = @FQuyen AND @FQuyen is not null)
	OR ([f_xoa] = @FXoa AND @FXoa is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Loai_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Loai_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Loai_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets all records from the Loai table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Loai_Get_List

AS


				
				SELECT
					[Id],
					[TenLoai],
					[GhiChu]
				FROM
					[dbo].[Loai]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Loai_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Loai_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Loai_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets records from the Loai table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Loai_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Id])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [Id]'
				SET @SQL = @SQL + ' FROM [dbo].[Loai]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[Id], O.[TenLoai], O.[GhiChu]
				FROM
				    [dbo].[Loai] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Id] = PageIndex.[Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Loai]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Loai_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Loai_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Loai_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Inserts a record into the Loai table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Loai_Insert
(

	@Id int   ,

	@TenLoai nvarchar (30)  ,

	@GhiChu nvarchar (255)  
)
AS


				
				INSERT INTO [dbo].[Loai]
					(
					[Id]
					,[TenLoai]
					,[GhiChu]
					)
				VALUES
					(
					@Id
					,@TenLoai
					,@GhiChu
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Loai_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Loai_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Loai_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Updates a record in the Loai table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Loai_Update
(

	@Id int   ,

	@OriginalId int   ,

	@TenLoai nvarchar (30)  ,

	@GhiChu nvarchar (255)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Loai]
				SET
					[Id] = @Id
					,[TenLoai] = @TenLoai
					,[GhiChu] = @GhiChu
				WHERE
[Id] = @OriginalId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Loai_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Loai_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Loai_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Deletes a record in the Loai table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Loai_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[Loai] WITH (ROWLOCK) 
				WHERE
					[Id] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Loai_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Loai_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Loai_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Select records from the Loai table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Loai_GetById
(

	@Id int   
)
AS


				SELECT
					[Id],
					[TenLoai],
					[GhiChu]
				FROM
					[dbo].[Loai]
				WHERE
					[Id] = @Id
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Loai_GetByTenLoai procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Loai_GetByTenLoai') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Loai_GetByTenLoai
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Select records from the Loai table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Loai_GetByTenLoai
(

	@TenLoai nvarchar (30)  
)
AS


				SELECT
					[Id],
					[TenLoai],
					[GhiChu]
				FROM
					[dbo].[Loai]
				WHERE
					[TenLoai] = @TenLoai
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Loai_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Loai_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Loai_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Finds records in the Loai table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Loai_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@TenLoai nvarchar (30)  = null ,

	@GhiChu nvarchar (255)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Id]
	, [TenLoai]
	, [GhiChu]
    FROM
	[dbo].[Loai]
    WHERE 
	 ([Id] = @Id OR @Id IS NULL)
	AND ([TenLoai] = @TenLoai OR @TenLoai IS NULL)
	AND ([GhiChu] = @GhiChu OR @GhiChu IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Id]
	, [TenLoai]
	, [GhiChu]
    FROM
	[dbo].[Loai]
    WHERE 
	 ([Id] = @Id AND @Id is not null)
	OR ([TenLoai] = @TenLoai AND @TenLoai is not null)
	OR ([GhiChu] = @GhiChu AND @GhiChu is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmqh_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmqh_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmqh_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets all records from the pdmqh table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmqh_Get_List

AS


				
				SELECT
					[Id],
					[maqh],
					[tenqh],
					[matp],
					[userud],
					[ngayud]
				FROM
					[dbo].[pdmqh]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmqh_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmqh_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmqh_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets records from the pdmqh table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmqh_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Id])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [Id]'
				SET @SQL = @SQL + ' FROM [dbo].[pdmqh]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[Id], O.[maqh], O.[tenqh], O.[matp], O.[userud], O.[ngayud]
				FROM
				    [dbo].[pdmqh] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Id] = PageIndex.[Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[pdmqh]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmqh_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmqh_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmqh_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Inserts a record into the pdmqh table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmqh_Insert
(

	@Id int    OUTPUT,

	@Maqh char (5)  ,

	@Tenqh nvarchar (50)  ,

	@Matp char (3)  ,

	@Userud char (10)  ,

	@Ngayud datetime   
)
AS


				
				INSERT INTO [dbo].[pdmqh]
					(
					[maqh]
					,[tenqh]
					,[matp]
					,[userud]
					,[ngayud]
					)
				VALUES
					(
					@Maqh
					,@Tenqh
					,@Matp
					,@Userud
					,@Ngayud
					)
				
				-- Get the identity value
				SET @Id = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmqh_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmqh_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmqh_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Updates a record in the pdmqh table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmqh_Update
(

	@Id int   ,

	@Maqh char (5)  ,

	@Tenqh nvarchar (50)  ,

	@Matp char (3)  ,

	@Userud char (10)  ,

	@Ngayud datetime   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[pdmqh]
				SET
					[maqh] = @Maqh
					,[tenqh] = @Tenqh
					,[matp] = @Matp
					,[userud] = @Userud
					,[ngayud] = @Ngayud
				WHERE
[Id] = @Id 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmqh_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmqh_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmqh_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Deletes a record in the pdmqh table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmqh_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[pdmqh] WITH (ROWLOCK) 
				WHERE
					[Id] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmqh_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmqh_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmqh_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Select records from the pdmqh table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmqh_GetById
(

	@Id int   
)
AS


				SELECT
					[Id],
					[maqh],
					[tenqh],
					[matp],
					[userud],
					[ngayud]
				FROM
					[dbo].[pdmqh]
				WHERE
					[Id] = @Id
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmqh_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmqh_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmqh_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Finds records in the pdmqh table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmqh_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@Maqh char (5)  = null ,

	@Tenqh nvarchar (50)  = null ,

	@Matp char (3)  = null ,

	@Userud char (10)  = null ,

	@Ngayud datetime   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Id]
	, [maqh]
	, [tenqh]
	, [matp]
	, [userud]
	, [ngayud]
    FROM
	[dbo].[pdmqh]
    WHERE 
	 ([Id] = @Id OR @Id IS NULL)
	AND ([maqh] = @Maqh OR @Maqh IS NULL)
	AND ([tenqh] = @Tenqh OR @Tenqh IS NULL)
	AND ([matp] = @Matp OR @Matp IS NULL)
	AND ([userud] = @Userud OR @Userud IS NULL)
	AND ([ngayud] = @Ngayud OR @Ngayud IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Id]
	, [maqh]
	, [tenqh]
	, [matp]
	, [userud]
	, [ngayud]
    FROM
	[dbo].[pdmqh]
    WHERE 
	 ([Id] = @Id AND @Id is not null)
	OR ([maqh] = @Maqh AND @Maqh is not null)
	OR ([tenqh] = @Tenqh AND @Tenqh is not null)
	OR ([matp] = @Matp AND @Matp is not null)
	OR ([userud] = @Userud AND @Userud is not null)
	OR ([ngayud] = @Ngayud AND @Ngayud is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.LichSuDieuTri_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.LichSuDieuTri_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.LichSuDieuTri_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets all records from the LichSuDieuTri table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.LichSuDieuTri_Get_List

AS


				
				SELECT
					[BenhNhanId],
					[MaOpc],
					[NgayBatDau],
					[NgayKetThuc],
					[MaKetThuc],
					[TuOpc],
					[OpcGoc],
					[NgayDangKyArv],
					[Id]
				FROM
					[dbo].[LichSuDieuTri]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.LichSuDieuTri_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.LichSuDieuTri_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.LichSuDieuTri_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets records from the LichSuDieuTri table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.LichSuDieuTri_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Id])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [Id]'
				SET @SQL = @SQL + ' FROM [dbo].[LichSuDieuTri]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[BenhNhanId], O.[MaOpc], O.[NgayBatDau], O.[NgayKetThuc], O.[MaKetThuc], O.[TuOpc], O.[OpcGoc], O.[NgayDangKyArv], O.[Id]
				FROM
				    [dbo].[LichSuDieuTri] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Id] = PageIndex.[Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[LichSuDieuTri]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.LichSuDieuTri_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.LichSuDieuTri_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.LichSuDieuTri_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Inserts a record into the LichSuDieuTri table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.LichSuDieuTri_Insert
(

	@BenhNhanId int   ,

	@MaOpc char (2)  ,

	@NgayBatDau datetime   ,

	@NgayKetThuc datetime   ,

	@MaKetThuc int   ,

	@TuOpc char (2)  ,

	@OpcGoc char (2)  ,

	@NgayDangKyArv datetime   ,

	@Id int    OUTPUT
)
AS


				
				INSERT INTO [dbo].[LichSuDieuTri]
					(
					[BenhNhanId]
					,[MaOpc]
					,[NgayBatDau]
					,[NgayKetThuc]
					,[MaKetThuc]
					,[TuOpc]
					,[OpcGoc]
					,[NgayDangKyArv]
					)
				VALUES
					(
					@BenhNhanId
					,@MaOpc
					,@NgayBatDau
					,@NgayKetThuc
					,@MaKetThuc
					,@TuOpc
					,@OpcGoc
					,@NgayDangKyArv
					)
				
				-- Get the identity value
				SET @Id = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.LichSuDieuTri_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.LichSuDieuTri_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.LichSuDieuTri_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Updates a record in the LichSuDieuTri table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.LichSuDieuTri_Update
(

	@BenhNhanId int   ,

	@MaOpc char (2)  ,

	@NgayBatDau datetime   ,

	@NgayKetThuc datetime   ,

	@MaKetThuc int   ,

	@TuOpc char (2)  ,

	@OpcGoc char (2)  ,

	@NgayDangKyArv datetime   ,

	@Id int   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[LichSuDieuTri]
				SET
					[BenhNhanId] = @BenhNhanId
					,[MaOpc] = @MaOpc
					,[NgayBatDau] = @NgayBatDau
					,[NgayKetThuc] = @NgayKetThuc
					,[MaKetThuc] = @MaKetThuc
					,[TuOpc] = @TuOpc
					,[OpcGoc] = @OpcGoc
					,[NgayDangKyArv] = @NgayDangKyArv
				WHERE
[Id] = @Id 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.LichSuDieuTri_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.LichSuDieuTri_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.LichSuDieuTri_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Deletes a record in the LichSuDieuTri table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.LichSuDieuTri_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[LichSuDieuTri] WITH (ROWLOCK) 
				WHERE
					[Id] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.LichSuDieuTri_GetByBenhNhanId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.LichSuDieuTri_GetByBenhNhanId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.LichSuDieuTri_GetByBenhNhanId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Select records from the LichSuDieuTri table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.LichSuDieuTri_GetByBenhNhanId
(

	@BenhNhanId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[BenhNhanId],
					[MaOpc],
					[NgayBatDau],
					[NgayKetThuc],
					[MaKetThuc],
					[TuOpc],
					[OpcGoc],
					[NgayDangKyArv],
					[Id]
				FROM
					[dbo].[LichSuDieuTri]
				WHERE
					[BenhNhanId] = @BenhNhanId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.LichSuDieuTri_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.LichSuDieuTri_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.LichSuDieuTri_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Select records from the LichSuDieuTri table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.LichSuDieuTri_GetById
(

	@Id int   
)
AS


				SELECT
					[BenhNhanId],
					[MaOpc],
					[NgayBatDau],
					[NgayKetThuc],
					[MaKetThuc],
					[TuOpc],
					[OpcGoc],
					[NgayDangKyArv],
					[Id]
				FROM
					[dbo].[LichSuDieuTri]
				WHERE
					[Id] = @Id
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.LichSuDieuTri_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.LichSuDieuTri_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.LichSuDieuTri_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Finds records in the LichSuDieuTri table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.LichSuDieuTri_Find
(

	@SearchUsingOR bit   = null ,

	@BenhNhanId int   = null ,

	@MaOpc char (2)  = null ,

	@NgayBatDau datetime   = null ,

	@NgayKetThuc datetime   = null ,

	@MaKetThuc int   = null ,

	@TuOpc char (2)  = null ,

	@OpcGoc char (2)  = null ,

	@NgayDangKyArv datetime   = null ,

	@Id int   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [BenhNhanId]
	, [MaOpc]
	, [NgayBatDau]
	, [NgayKetThuc]
	, [MaKetThuc]
	, [TuOpc]
	, [OpcGoc]
	, [NgayDangKyArv]
	, [Id]
    FROM
	[dbo].[LichSuDieuTri]
    WHERE 
	 ([BenhNhanId] = @BenhNhanId OR @BenhNhanId IS NULL)
	AND ([MaOpc] = @MaOpc OR @MaOpc IS NULL)
	AND ([NgayBatDau] = @NgayBatDau OR @NgayBatDau IS NULL)
	AND ([NgayKetThuc] = @NgayKetThuc OR @NgayKetThuc IS NULL)
	AND ([MaKetThuc] = @MaKetThuc OR @MaKetThuc IS NULL)
	AND ([TuOpc] = @TuOpc OR @TuOpc IS NULL)
	AND ([OpcGoc] = @OpcGoc OR @OpcGoc IS NULL)
	AND ([NgayDangKyArv] = @NgayDangKyArv OR @NgayDangKyArv IS NULL)
	AND ([Id] = @Id OR @Id IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [BenhNhanId]
	, [MaOpc]
	, [NgayBatDau]
	, [NgayKetThuc]
	, [MaKetThuc]
	, [TuOpc]
	, [OpcGoc]
	, [NgayDangKyArv]
	, [Id]
    FROM
	[dbo].[LichSuDieuTri]
    WHERE 
	 ([BenhNhanId] = @BenhNhanId AND @BenhNhanId is not null)
	OR ([MaOpc] = @MaOpc AND @MaOpc is not null)
	OR ([NgayBatDau] = @NgayBatDau AND @NgayBatDau is not null)
	OR ([NgayKetThuc] = @NgayKetThuc AND @NgayKetThuc is not null)
	OR ([MaKetThuc] = @MaKetThuc AND @MaKetThuc is not null)
	OR ([TuOpc] = @TuOpc AND @TuOpc is not null)
	OR ([OpcGoc] = @OpcGoc AND @OpcGoc is not null)
	OR ([NgayDangKyArv] = @NgayDangKyArv AND @NgayDangKyArv is not null)
	OR ([Id] = @Id AND @Id is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChuyenOpcBuffer_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChuyenOpcBuffer_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChuyenOpcBuffer_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets all records from the ChuyenOpcBuffer table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChuyenOpcBuffer_Get_List

AS


				
				SELECT
					[BenhNhanId],
					[MaOpc],
					[Ngay],
					[ToiOpc]
				FROM
					[dbo].[ChuyenOpcBuffer]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChuyenOpcBuffer_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChuyenOpcBuffer_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChuyenOpcBuffer_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets records from the ChuyenOpcBuffer table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChuyenOpcBuffer_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [BenhNhanId] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([BenhNhanId])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [BenhNhanId]'
				SET @SQL = @SQL + ' FROM [dbo].[ChuyenOpcBuffer]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[BenhNhanId], O.[MaOpc], O.[Ngay], O.[ToiOpc]
				FROM
				    [dbo].[ChuyenOpcBuffer] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[BenhNhanId] = PageIndex.[BenhNhanId]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[ChuyenOpcBuffer]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChuyenOpcBuffer_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChuyenOpcBuffer_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChuyenOpcBuffer_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Inserts a record into the ChuyenOpcBuffer table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChuyenOpcBuffer_Insert
(

	@BenhNhanId int   ,

	@MaOpc char (2)  ,

	@Ngay datetime   ,

	@ToiOpc char (2)  
)
AS


				
				INSERT INTO [dbo].[ChuyenOpcBuffer]
					(
					[BenhNhanId]
					,[MaOpc]
					,[Ngay]
					,[ToiOpc]
					)
				VALUES
					(
					@BenhNhanId
					,@MaOpc
					,@Ngay
					,@ToiOpc
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChuyenOpcBuffer_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChuyenOpcBuffer_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChuyenOpcBuffer_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Updates a record in the ChuyenOpcBuffer table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChuyenOpcBuffer_Update
(

	@BenhNhanId int   ,

	@OriginalBenhNhanId int   ,

	@MaOpc char (2)  ,

	@Ngay datetime   ,

	@ToiOpc char (2)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[ChuyenOpcBuffer]
				SET
					[BenhNhanId] = @BenhNhanId
					,[MaOpc] = @MaOpc
					,[Ngay] = @Ngay
					,[ToiOpc] = @ToiOpc
				WHERE
[BenhNhanId] = @OriginalBenhNhanId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChuyenOpcBuffer_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChuyenOpcBuffer_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChuyenOpcBuffer_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Deletes a record in the ChuyenOpcBuffer table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChuyenOpcBuffer_Delete
(

	@BenhNhanId int   
)
AS


				DELETE FROM [dbo].[ChuyenOpcBuffer] WITH (ROWLOCK) 
				WHERE
					[BenhNhanId] = @BenhNhanId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChuyenOpcBuffer_GetByBenhNhanId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChuyenOpcBuffer_GetByBenhNhanId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChuyenOpcBuffer_GetByBenhNhanId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Select records from the ChuyenOpcBuffer table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChuyenOpcBuffer_GetByBenhNhanId
(

	@BenhNhanId int   
)
AS


				SELECT
					[BenhNhanId],
					[MaOpc],
					[Ngay],
					[ToiOpc]
				FROM
					[dbo].[ChuyenOpcBuffer]
				WHERE
					[BenhNhanId] = @BenhNhanId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ChuyenOpcBuffer_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ChuyenOpcBuffer_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ChuyenOpcBuffer_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Finds records in the ChuyenOpcBuffer table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ChuyenOpcBuffer_Find
(

	@SearchUsingOR bit   = null ,

	@BenhNhanId int   = null ,

	@MaOpc char (2)  = null ,

	@Ngay datetime   = null ,

	@ToiOpc char (2)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [BenhNhanId]
	, [MaOpc]
	, [Ngay]
	, [ToiOpc]
    FROM
	[dbo].[ChuyenOpcBuffer]
    WHERE 
	 ([BenhNhanId] = @BenhNhanId OR @BenhNhanId IS NULL)
	AND ([MaOpc] = @MaOpc OR @MaOpc IS NULL)
	AND ([Ngay] = @Ngay OR @Ngay IS NULL)
	AND ([ToiOpc] = @ToiOpc OR @ToiOpc IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [BenhNhanId]
	, [MaOpc]
	, [Ngay]
	, [ToiOpc]
    FROM
	[dbo].[ChuyenOpcBuffer]
    WHERE 
	 ([BenhNhanId] = @BenhNhanId AND @BenhNhanId is not null)
	OR ([MaOpc] = @MaOpc AND @MaOpc is not null)
	OR ([Ngay] = @Ngay AND @Ngay is not null)
	OR ([ToiOpc] = @ToiOpc AND @ToiOpc is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DieuTri_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DieuTri_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DieuTri_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets all records from the DieuTri table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DieuTri_Get_List

AS


				
				SELECT
					[Id],
					[BenhNhanId],
					[DieuTriArv],
					[NgayTiepNhanDieuTri],
					[UserTiepNhan],
					[MaChamDutDieuTri],
					[NgayChamDutDieuTri],
					[UserChamDut],
					[MaOpc],
					[DeleteFlag]
				FROM
					[dbo].[DieuTri]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DieuTri_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DieuTri_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DieuTri_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets records from the DieuTri table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DieuTri_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Id])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [Id]'
				SET @SQL = @SQL + ' FROM [dbo].[DieuTri]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[Id], O.[BenhNhanId], O.[DieuTriArv], O.[NgayTiepNhanDieuTri], O.[UserTiepNhan], O.[MaChamDutDieuTri], O.[NgayChamDutDieuTri], O.[UserChamDut], O.[MaOpc], O.[DeleteFlag]
				FROM
				    [dbo].[DieuTri] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Id] = PageIndex.[Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[DieuTri]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DieuTri_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DieuTri_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DieuTri_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Inserts a record into the DieuTri table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DieuTri_Insert
(

	@Id int    OUTPUT,

	@BenhNhanId int   ,

	@DieuTriArv bit   ,

	@NgayTiepNhanDieuTri datetime   ,

	@UserTiepNhan nvarchar (30)  ,

	@MaChamDutDieuTri int   ,

	@NgayChamDutDieuTri datetime   ,

	@UserChamDut nvarchar (30)  ,

	@MaOpc char (2)  ,

	@DeleteFlag bit   
)
AS


				
				INSERT INTO [dbo].[DieuTri]
					(
					[BenhNhanId]
					,[DieuTriArv]
					,[NgayTiepNhanDieuTri]
					,[UserTiepNhan]
					,[MaChamDutDieuTri]
					,[NgayChamDutDieuTri]
					,[UserChamDut]
					,[MaOpc]
					,[DeleteFlag]
					)
				VALUES
					(
					@BenhNhanId
					,@DieuTriArv
					,@NgayTiepNhanDieuTri
					,@UserTiepNhan
					,@MaChamDutDieuTri
					,@NgayChamDutDieuTri
					,@UserChamDut
					,@MaOpc
					,@DeleteFlag
					)
				
				-- Get the identity value
				SET @Id = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DieuTri_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DieuTri_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DieuTri_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Updates a record in the DieuTri table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DieuTri_Update
(

	@Id int   ,

	@BenhNhanId int   ,

	@DieuTriArv bit   ,

	@NgayTiepNhanDieuTri datetime   ,

	@UserTiepNhan nvarchar (30)  ,

	@MaChamDutDieuTri int   ,

	@NgayChamDutDieuTri datetime   ,

	@UserChamDut nvarchar (30)  ,

	@MaOpc char (2)  ,

	@DeleteFlag bit   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[DieuTri]
				SET
					[BenhNhanId] = @BenhNhanId
					,[DieuTriArv] = @DieuTriArv
					,[NgayTiepNhanDieuTri] = @NgayTiepNhanDieuTri
					,[UserTiepNhan] = @UserTiepNhan
					,[MaChamDutDieuTri] = @MaChamDutDieuTri
					,[NgayChamDutDieuTri] = @NgayChamDutDieuTri
					,[UserChamDut] = @UserChamDut
					,[MaOpc] = @MaOpc
					,[DeleteFlag] = @DeleteFlag
				WHERE
[Id] = @Id 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DieuTri_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DieuTri_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DieuTri_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Deletes a record in the DieuTri table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DieuTri_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[DieuTri] WITH (ROWLOCK) 
				WHERE
					[Id] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DieuTri_GetByBenhNhanId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DieuTri_GetByBenhNhanId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DieuTri_GetByBenhNhanId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Select records from the DieuTri table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DieuTri_GetByBenhNhanId
(

	@BenhNhanId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[Id],
					[BenhNhanId],
					[DieuTriArv],
					[NgayTiepNhanDieuTri],
					[UserTiepNhan],
					[MaChamDutDieuTri],
					[NgayChamDutDieuTri],
					[UserChamDut],
					[MaOpc],
					[DeleteFlag]
				FROM
					[dbo].[DieuTri]
				WHERE
					[BenhNhanId] = @BenhNhanId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DieuTri_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DieuTri_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DieuTri_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Select records from the DieuTri table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DieuTri_GetById
(

	@Id int   
)
AS


				SELECT
					[Id],
					[BenhNhanId],
					[DieuTriArv],
					[NgayTiepNhanDieuTri],
					[UserTiepNhan],
					[MaChamDutDieuTri],
					[NgayChamDutDieuTri],
					[UserChamDut],
					[MaOpc],
					[DeleteFlag]
				FROM
					[dbo].[DieuTri]
				WHERE
					[Id] = @Id
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DieuTri_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DieuTri_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DieuTri_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Finds records in the DieuTri table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DieuTri_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@BenhNhanId int   = null ,

	@DieuTriArv bit   = null ,

	@NgayTiepNhanDieuTri datetime   = null ,

	@UserTiepNhan nvarchar (30)  = null ,

	@MaChamDutDieuTri int   = null ,

	@NgayChamDutDieuTri datetime   = null ,

	@UserChamDut nvarchar (30)  = null ,

	@MaOpc char (2)  = null ,

	@DeleteFlag bit   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Id]
	, [BenhNhanId]
	, [DieuTriArv]
	, [NgayTiepNhanDieuTri]
	, [UserTiepNhan]
	, [MaChamDutDieuTri]
	, [NgayChamDutDieuTri]
	, [UserChamDut]
	, [MaOpc]
	, [DeleteFlag]
    FROM
	[dbo].[DieuTri]
    WHERE 
	 ([Id] = @Id OR @Id IS NULL)
	AND ([BenhNhanId] = @BenhNhanId OR @BenhNhanId IS NULL)
	AND ([DieuTriArv] = @DieuTriArv OR @DieuTriArv IS NULL)
	AND ([NgayTiepNhanDieuTri] = @NgayTiepNhanDieuTri OR @NgayTiepNhanDieuTri IS NULL)
	AND ([UserTiepNhan] = @UserTiepNhan OR @UserTiepNhan IS NULL)
	AND ([MaChamDutDieuTri] = @MaChamDutDieuTri OR @MaChamDutDieuTri IS NULL)
	AND ([NgayChamDutDieuTri] = @NgayChamDutDieuTri OR @NgayChamDutDieuTri IS NULL)
	AND ([UserChamDut] = @UserChamDut OR @UserChamDut IS NULL)
	AND ([MaOpc] = @MaOpc OR @MaOpc IS NULL)
	AND ([DeleteFlag] = @DeleteFlag OR @DeleteFlag IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Id]
	, [BenhNhanId]
	, [DieuTriArv]
	, [NgayTiepNhanDieuTri]
	, [UserTiepNhan]
	, [MaChamDutDieuTri]
	, [NgayChamDutDieuTri]
	, [UserChamDut]
	, [MaOpc]
	, [DeleteFlag]
    FROM
	[dbo].[DieuTri]
    WHERE 
	 ([Id] = @Id AND @Id is not null)
	OR ([BenhNhanId] = @BenhNhanId AND @BenhNhanId is not null)
	OR ([DieuTriArv] = @DieuTriArv AND @DieuTriArv is not null)
	OR ([NgayTiepNhanDieuTri] = @NgayTiepNhanDieuTri AND @NgayTiepNhanDieuTri is not null)
	OR ([UserTiepNhan] = @UserTiepNhan AND @UserTiepNhan is not null)
	OR ([MaChamDutDieuTri] = @MaChamDutDieuTri AND @MaChamDutDieuTri is not null)
	OR ([NgayChamDutDieuTri] = @NgayChamDutDieuTri AND @NgayChamDutDieuTri is not null)
	OR ([UserChamDut] = @UserChamDut AND @UserChamDut is not null)
	OR ([MaOpc] = @MaOpc AND @MaOpc is not null)
	OR ([DeleteFlag] = @DeleteFlag AND @DeleteFlag is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmphxa_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmphxa_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmphxa_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets all records from the pdmphxa table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmphxa_Get_List

AS


				
				SELECT
					[Id],
					[mapx],
					[tenpx],
					[maqh],
					[userud],
					[ngayud]
				FROM
					[dbo].[pdmphxa]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmphxa_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmphxa_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmphxa_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets records from the pdmphxa table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmphxa_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Id])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [Id]'
				SET @SQL = @SQL + ' FROM [dbo].[pdmphxa]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[Id], O.[mapx], O.[tenpx], O.[maqh], O.[userud], O.[ngayud]
				FROM
				    [dbo].[pdmphxa] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Id] = PageIndex.[Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[pdmphxa]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmphxa_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmphxa_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmphxa_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Inserts a record into the pdmphxa table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmphxa_Insert
(

	@Id int    OUTPUT,

	@Mapx char (8)  ,

	@Tenpx nvarchar (50)  ,

	@Maqh char (5)  ,

	@Userud char (10)  ,

	@Ngayud datetime   
)
AS


				
				INSERT INTO [dbo].[pdmphxa]
					(
					[mapx]
					,[tenpx]
					,[maqh]
					,[userud]
					,[ngayud]
					)
				VALUES
					(
					@Mapx
					,@Tenpx
					,@Maqh
					,@Userud
					,@Ngayud
					)
				
				-- Get the identity value
				SET @Id = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmphxa_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmphxa_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmphxa_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Updates a record in the pdmphxa table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmphxa_Update
(

	@Id int   ,

	@Mapx char (8)  ,

	@Tenpx nvarchar (50)  ,

	@Maqh char (5)  ,

	@Userud char (10)  ,

	@Ngayud datetime   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[pdmphxa]
				SET
					[mapx] = @Mapx
					,[tenpx] = @Tenpx
					,[maqh] = @Maqh
					,[userud] = @Userud
					,[ngayud] = @Ngayud
				WHERE
[Id] = @Id 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmphxa_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmphxa_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmphxa_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Deletes a record in the pdmphxa table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmphxa_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[pdmphxa] WITH (ROWLOCK) 
				WHERE
					[Id] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmphxa_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmphxa_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmphxa_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Select records from the pdmphxa table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmphxa_GetById
(

	@Id int   
)
AS


				SELECT
					[Id],
					[mapx],
					[tenpx],
					[maqh],
					[userud],
					[ngayud]
				FROM
					[dbo].[pdmphxa]
				WHERE
					[Id] = @Id
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmphxa_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmphxa_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmphxa_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Finds records in the pdmphxa table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmphxa_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@Mapx char (8)  = null ,

	@Tenpx nvarchar (50)  = null ,

	@Maqh char (5)  = null ,

	@Userud char (10)  = null ,

	@Ngayud datetime   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Id]
	, [mapx]
	, [tenpx]
	, [maqh]
	, [userud]
	, [ngayud]
    FROM
	[dbo].[pdmphxa]
    WHERE 
	 ([Id] = @Id OR @Id IS NULL)
	AND ([mapx] = @Mapx OR @Mapx IS NULL)
	AND ([tenpx] = @Tenpx OR @Tenpx IS NULL)
	AND ([maqh] = @Maqh OR @Maqh IS NULL)
	AND ([userud] = @Userud OR @Userud IS NULL)
	AND ([ngayud] = @Ngayud OR @Ngayud IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Id]
	, [mapx]
	, [tenpx]
	, [maqh]
	, [userud]
	, [ngayud]
    FROM
	[dbo].[pdmphxa]
    WHERE 
	 ([Id] = @Id AND @Id is not null)
	OR ([mapx] = @Mapx AND @Mapx is not null)
	OR ([tenpx] = @Tenpx AND @Tenpx is not null)
	OR ([maqh] = @Maqh AND @Maqh is not null)
	OR ([userud] = @Userud AND @Userud is not null)
	OR ([ngayud] = @Ngayud AND @Ngayud is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmdt_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmdt_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmdt_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets all records from the pdmdt table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmdt_Get_List

AS


				
				SELECT
					[Id],
					[madt],
					[tendt]
				FROM
					[dbo].[pdmdt]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmdt_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmdt_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmdt_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets records from the pdmdt table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmdt_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Id])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [Id]'
				SET @SQL = @SQL + ' FROM [dbo].[pdmdt]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[Id], O.[madt], O.[tendt]
				FROM
				    [dbo].[pdmdt] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Id] = PageIndex.[Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[pdmdt]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmdt_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmdt_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmdt_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Inserts a record into the pdmdt table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmdt_Insert
(

	@Id int    OUTPUT,

	@Madt int   ,

	@Tendt nvarchar (50)  
)
AS


				
				INSERT INTO [dbo].[pdmdt]
					(
					[madt]
					,[tendt]
					)
				VALUES
					(
					@Madt
					,@Tendt
					)
				
				-- Get the identity value
				SET @Id = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmdt_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmdt_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmdt_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Updates a record in the pdmdt table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmdt_Update
(

	@Id int   ,

	@Madt int   ,

	@Tendt nvarchar (50)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[pdmdt]
				SET
					[madt] = @Madt
					,[tendt] = @Tendt
				WHERE
[Id] = @Id 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmdt_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmdt_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmdt_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Deletes a record in the pdmdt table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmdt_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[pdmdt] WITH (ROWLOCK) 
				WHERE
					[Id] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmdt_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmdt_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmdt_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Select records from the pdmdt table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmdt_GetById
(

	@Id int   
)
AS


				SELECT
					[Id],
					[madt],
					[tendt]
				FROM
					[dbo].[pdmdt]
				WHERE
					[Id] = @Id
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmdt_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmdt_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmdt_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Finds records in the pdmdt table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmdt_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@Madt int   = null ,

	@Tendt nvarchar (50)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Id]
	, [madt]
	, [tendt]
    FROM
	[dbo].[pdmdt]
    WHERE 
	 ([Id] = @Id OR @Id IS NULL)
	AND ([madt] = @Madt OR @Madt IS NULL)
	AND ([tendt] = @Tendt OR @Tendt IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Id]
	, [madt]
	, [tendt]
    FROM
	[dbo].[pdmdt]
    WHERE 
	 ([Id] = @Id AND @Id is not null)
	OR ([madt] = @Madt AND @Madt is not null)
	OR ([tendt] = @Tendt AND @Tendt is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmnn_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmnn_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmnn_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets all records from the pdmnn table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmnn_Get_List

AS


				
				SELECT
					[Id],
					[mann],
					[tennn],
					[userud],
					[ngayud]
				FROM
					[dbo].[pdmnn]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmnn_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmnn_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmnn_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets records from the pdmnn table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmnn_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Id])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [Id]'
				SET @SQL = @SQL + ' FROM [dbo].[pdmnn]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[Id], O.[mann], O.[tennn], O.[userud], O.[ngayud]
				FROM
				    [dbo].[pdmnn] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Id] = PageIndex.[Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[pdmnn]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmnn_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmnn_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmnn_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Inserts a record into the pdmnn table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmnn_Insert
(

	@Id int    OUTPUT,

	@Mann char (3)  ,

	@Tennn nvarchar (50)  ,

	@Userud char (10)  ,

	@Ngayud datetime   
)
AS


				
				INSERT INTO [dbo].[pdmnn]
					(
					[mann]
					,[tennn]
					,[userud]
					,[ngayud]
					)
				VALUES
					(
					@Mann
					,@Tennn
					,@Userud
					,@Ngayud
					)
				
				-- Get the identity value
				SET @Id = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmnn_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmnn_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmnn_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Updates a record in the pdmnn table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmnn_Update
(

	@Id int   ,

	@Mann char (3)  ,

	@Tennn nvarchar (50)  ,

	@Userud char (10)  ,

	@Ngayud datetime   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[pdmnn]
				SET
					[mann] = @Mann
					,[tennn] = @Tennn
					,[userud] = @Userud
					,[ngayud] = @Ngayud
				WHERE
[Id] = @Id 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmnn_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmnn_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmnn_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Deletes a record in the pdmnn table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmnn_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[pdmnn] WITH (ROWLOCK) 
				WHERE
					[Id] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmnn_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmnn_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmnn_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Select records from the pdmnn table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmnn_GetById
(

	@Id int   
)
AS


				SELECT
					[Id],
					[mann],
					[tennn],
					[userud],
					[ngayud]
				FROM
					[dbo].[pdmnn]
				WHERE
					[Id] = @Id
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmnn_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmnn_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmnn_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Finds records in the pdmnn table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmnn_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@Mann char (3)  = null ,

	@Tennn nvarchar (50)  = null ,

	@Userud char (10)  = null ,

	@Ngayud datetime   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Id]
	, [mann]
	, [tennn]
	, [userud]
	, [ngayud]
    FROM
	[dbo].[pdmnn]
    WHERE 
	 ([Id] = @Id OR @Id IS NULL)
	AND ([mann] = @Mann OR @Mann IS NULL)
	AND ([tennn] = @Tennn OR @Tennn IS NULL)
	AND ([userud] = @Userud OR @Userud IS NULL)
	AND ([ngayud] = @Ngayud OR @Ngayud IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Id]
	, [mann]
	, [tennn]
	, [userud]
	, [ngayud]
    FROM
	[dbo].[pdmnn]
    WHERE 
	 ([Id] = @Id AND @Id is not null)
	OR ([mann] = @Mann AND @Mann is not null)
	OR ([tennn] = @Tennn AND @Tennn is not null)
	OR ([userud] = @Userud AND @Userud is not null)
	OR ([ngayud] = @Ngayud AND @Ngayud is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_tinh_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_tinh_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_tinh_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets all records from the ut_tinh table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_tinh_Get_List

AS


				
				SELECT
					[Id],
					[f_matinh],
					[f_tentinh]
				FROM
					[dbo].[ut_tinh]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_tinh_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_tinh_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_tinh_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets records from the ut_tinh table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_tinh_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Id])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [Id]'
				SET @SQL = @SQL + ' FROM [dbo].[ut_tinh]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[Id], O.[f_matinh], O.[f_tentinh]
				FROM
				    [dbo].[ut_tinh] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Id] = PageIndex.[Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[ut_tinh]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_tinh_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_tinh_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_tinh_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Inserts a record into the ut_tinh table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_tinh_Insert
(

	@Id int    OUTPUT,

	@FMatinh nvarchar (50)  ,

	@FTentinh nvarchar (50)  
)
AS


				
				INSERT INTO [dbo].[ut_tinh]
					(
					[f_matinh]
					,[f_tentinh]
					)
				VALUES
					(
					@FMatinh
					,@FTentinh
					)
				
				-- Get the identity value
				SET @Id = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_tinh_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_tinh_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_tinh_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Updates a record in the ut_tinh table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_tinh_Update
(

	@Id int   ,

	@FMatinh nvarchar (50)  ,

	@FTentinh nvarchar (50)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[ut_tinh]
				SET
					[f_matinh] = @FMatinh
					,[f_tentinh] = @FTentinh
				WHERE
[Id] = @Id 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_tinh_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_tinh_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_tinh_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Deletes a record in the ut_tinh table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_tinh_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[ut_tinh] WITH (ROWLOCK) 
				WHERE
					[Id] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_tinh_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_tinh_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_tinh_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Select records from the ut_tinh table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_tinh_GetById
(

	@Id int   
)
AS


				SELECT
					[Id],
					[f_matinh],
					[f_tentinh]
				FROM
					[dbo].[ut_tinh]
				WHERE
					[Id] = @Id
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ut_tinh_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ut_tinh_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ut_tinh_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Finds records in the ut_tinh table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ut_tinh_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@FMatinh nvarchar (50)  = null ,

	@FTentinh nvarchar (50)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Id]
	, [f_matinh]
	, [f_tentinh]
    FROM
	[dbo].[ut_tinh]
    WHERE 
	 ([Id] = @Id OR @Id IS NULL)
	AND ([f_matinh] = @FMatinh OR @FMatinh IS NULL)
	AND ([f_tentinh] = @FTentinh OR @FTentinh IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Id]
	, [f_matinh]
	, [f_tentinh]
    FROM
	[dbo].[ut_tinh]
    WHERE 
	 ([Id] = @Id AND @Id is not null)
	OR ([f_matinh] = @FMatinh AND @FMatinh is not null)
	OR ([f_tentinh] = @FTentinh AND @FTentinh is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmncln_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmncln_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmncln_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets all records from the pdmncln table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmncln_Get_List

AS


				
				SELECT
					[Id],
					[mancln],
					[tenncln]
				FROM
					[dbo].[pdmncln]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmncln_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmncln_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmncln_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Gets records from the pdmncln table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmncln_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [Id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([Id])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [Id]'
				SET @SQL = @SQL + ' FROM [dbo].[pdmncln]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[Id], O.[mancln], O.[tenncln]
				FROM
				    [dbo].[pdmncln] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[Id] = PageIndex.[Id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[pdmncln]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmncln_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmncln_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmncln_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Inserts a record into the pdmncln table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmncln_Insert
(

	@Id int    OUTPUT,

	@Mancln smallint   ,

	@Tenncln nvarchar (50)  
)
AS


				
				INSERT INTO [dbo].[pdmncln]
					(
					[mancln]
					,[tenncln]
					)
				VALUES
					(
					@Mancln
					,@Tenncln
					)
				
				-- Get the identity value
				SET @Id = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmncln_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmncln_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmncln_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Updates a record in the pdmncln table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmncln_Update
(

	@Id int   ,

	@Mancln smallint   ,

	@Tenncln nvarchar (50)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[pdmncln]
				SET
					[mancln] = @Mancln
					,[tenncln] = @Tenncln
				WHERE
[Id] = @Id 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmncln_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmncln_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmncln_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Deletes a record in the pdmncln table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmncln_Delete
(

	@Id int   
)
AS


				DELETE FROM [dbo].[pdmncln] WITH (ROWLOCK) 
				WHERE
					[Id] = @Id
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmncln_GetById procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmncln_GetById') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmncln_GetById
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Select records from the pdmncln table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmncln_GetById
(

	@Id int   
)
AS


				SELECT
					[Id],
					[mancln],
					[tenncln]
				FROM
					[dbo].[pdmncln]
				WHERE
					[Id] = @Id
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.pdmncln_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.pdmncln_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.pdmncln_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: InfoExpress ()
-- Purpose: Finds records in the pdmncln table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.pdmncln_Find
(

	@SearchUsingOR bit   = null ,

	@Id int   = null ,

	@Mancln smallint   = null ,

	@Tenncln nvarchar (50)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [Id]
	, [mancln]
	, [tenncln]
    FROM
	[dbo].[pdmncln]
    WHERE 
	 ([Id] = @Id OR @Id IS NULL)
	AND ([mancln] = @Mancln OR @Mancln IS NULL)
	AND ([tenncln] = @Tenncln OR @Tenncln IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [Id]
	, [mancln]
	, [tenncln]
    FROM
	[dbo].[pdmncln]
    WHERE 
	 ([Id] = @Id AND @Id is not null)
	OR ([mancln] = @Mancln AND @Mancln is not null)
	OR ([tenncln] = @Tenncln AND @Tenncln is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

